//
//  MyKaQuanCell.m
//  FD
//
//  Created by Yasin on 2018/10/18.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
// 2:58--3:30

#import "MyKaQuanCell.h"
//model
#import "MyCouponModel.h"
#import "MyActivityRecomendModel.h"

/**2019-2-26复用cell 不公用model*/
#import "AvaliableCouponModel.h"

@interface MyKaQuanCell()
/**背景view*/
@property (nonatomic,  strong) UIView *bgView;
/**背景图片*/
@property (nonatomic,  strong) UIImageView *bgImageView;

/**1.icon 图标*/
@property (nonatomic,  strong) UIImageView *iconIv;
/**2.title 新人礼 金桔柠檬大杯兑换券任意消费得*/
@property (nonatomic,  strong) UILabel *titleLb;
/**3.类型lb(外送、门店用餐、)*/
@property (nonatomic,  strong) UILabel *typeLb;
/**灰色的线*/
@property (nonatomic, strong) UIView *lightGrayLineView;

/**4.有效期时间 lb*/
@property (nonatomic,  strong) UILabel *timeLb;
/**5.标题按钮(去外送点餐、去预约区号)*/
@property (nonatomic,  strong) UIButton *titleButton;
/**6.优惠券即将过期 */
@property (nonatomic,  strong) UILabel *beAboutToExpireLb;

@end

@implementation MyKaQuanCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    self.contentView.backgroundColor = kMainLightGrayColor; //灰色
    
    //取消点击
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.iconIv]; //图标
    [self.bgView addSubview:self.titleLb];//标题
//    [self.bgView addSubview:self.typeLb]; //类型(外送、门店用餐)
    [self.bgView addSubview:self.lightGrayLineView]; //线
    
    [self.bgView addSubview:self.timeLb]; //有效期 2018-10-10 2018-10-20
    [self.bgView addSubview:self.beAboutToExpireLb]; // 10-22
    [self.bgView addSubview:self.titleButton]; // 标签按钮(去外送点餐、去预约区号)
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //    self.bgView
    //    self.iconIv
    //    self.titleLb
    //    self.timeLb
    //    self.beAboutToExpireLb
    //    self.titleButton
    
    //背景view
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.top.equalTo(self.bgView.mas_top).offset(10);
    }];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(10);
        make.top.equalTo(self.iconIv.mas_top).offset(5);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.height.mas_equalTo(42);
    }];
    //线
    [self.lightGrayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.titleLb);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.titleLb.mas_bottom);
    }];
    
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.titleLb);
        make.height.mas_equalTo(30);
        make.top.equalTo(self.titleLb.mas_bottom).offset(2);
    }];
    
//    [self.beAboutToExpireLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top);
//    }];
    
    [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.titleLb);
        make.height.mas_equalTo(35);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-10);
    }];
}

//!!!!:Getter

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.borderColor = kMainLightGrayColor.CGColor;
        _bgView.layer.borderWidth = 1.0;
    }
    return _bgView;
}

- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [[UIImageView alloc] init];
        _iconIv.image = [UIImage imageNamed:@"发现cell图标"];
    }
    return _iconIv;
}
- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.text = @"新人礼 金桔柠檬大杯兑换券任意消费得";
        _titleLb.numberOfLines = 0;
//        _titleLb.font = [UIFont systemFontOfSize:16.0*kScaleFit];
    }
    return _titleLb;
}
- (UILabel *)typeLb {
    if (!_typeLb) {
        _typeLb = [UILabel new];
        _typeLb.font = [UIFont systemFontOfSize:12.0];
        _typeLb.textColor = [UIColor whiteColor];
        _typeLb.textAlignment = NSTextAlignmentCenter;
        _typeLb.backgroundColor = [UIColor redColor];
        _typeLb.layer.cornerRadius = 5;
        _typeLb.layer.masksToBounds = YES;
        _typeLb.text = @"门店用餐";
    }
    return _typeLb;
}

//线
- (UIView *)lightGrayLineView {
    if (!_lightGrayLineView) {
        _lightGrayLineView = [UIView new];
        _lightGrayLineView.backgroundColor = kMainLightGrayColor;
    }
    return _lightGrayLineView;
}

- (UILabel *)timeLb {
    if (!_timeLb) {
        _timeLb = [UILabel new];
        _timeLb.text = @"有效期:2018-01-21 至 2018-11-11";
        _timeLb.textColor = [UIColor lightGrayColor];
        _timeLb.font = [UIFont systemFontOfSize:12.0 * kScaleFit];
        _timeLb.numberOfLines = 0; //允许换行
    }
    return _timeLb;
}
- (UIButton *)titleButton {
    if (!_titleButton) {
        _titleButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        //国际化
        NSString *qssdcStr = NSLocalizedString(QuWaiSongDianCan, @"去外送点餐");
        [_titleButton setTitle:qssdcStr forState:(UIControlStateNormal)];
        [_titleButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _titleButton.backgroundColor = kMainRedColor;
        _titleButton.layer.masksToBounds = YES;
        _titleButton.layer.cornerRadius = 2;
        [_titleButton addTarget:self action:@selector(clickTitleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _titleButton;
}

- (UILabel *)beAboutToExpireLb {
    if (!_beAboutToExpireLb) {
        _beAboutToExpireLb = [UILabel new];
        
        //国际化
        _beAboutToExpireLb.text = NSLocalizedString(YouHuiQuanJiJiangGuoQi, @"优惠券即将到期");
        _beAboutToExpireLb.textColor = [UIColor redColor];
        _beAboutToExpireLb.font = [UIFont systemFontOfSize:12.0*kScaleFit];
//        _beAboutToExpireLb.backgroundColor = [UIColor greenColor];
    }
    return _beAboutToExpireLb;
}


#pragma mark - 点击事件
- (void)clickTitleButtonAction:(UIButton *)button {
    if (self.myKaQuanCellClickWaiSongDianCanBlock) {
        self.myKaQuanCellClickWaiSongDianCanBlock();
    }
    
    
    //2019-2-26 可用优惠券模型对应的点击事件
    if (self.clickAvaliableCouponUseNowBlock) {
        self.clickAvaliableCouponUseNowBlock(self.avaliableModel);
    }
}



//!!!!:设置数据
- (void)setModel:(MyCouponModel *)model {
    _model = model;
    
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];

    //国际化
    if ([NSString rb_ChineseLanguage]) {
        self.timeLb.text = [NSString stringWithFormat:@"有效期:%@ 至 %@",model.starTimeStr,model.endTimeStr];
    }else {
        self.timeLb.text = [NSString stringWithFormat:@"period of validity:%@ ~ %@",model.starTimeStr,model.endTimeStr];
    }
    
    self.titleLb.text = model.title;
    
//    model.isBeAboutToExpire = YES;
    if (model.isBeAboutToExpire) {
        self.beAboutToExpireLb.hidden = NO;//显示
    }else {
        self.beAboutToExpireLb.hidden = YES; //隐藏
    }
}

- (void)setActivityRecomendModel:(MyActivityRecomendModel *)activityRecomendModel {
    _activityRecomendModel = activityRecomendModel;
    //0.图标
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:activityRecomendModel.thumbnail]];


    //1.时间:国际化
    if ([NSString rb_ChineseLanguage]) {
        self.timeLb.text = [NSString stringWithFormat:@"有效期:%@ 至 %@",activityRecomendModel.activeTimeStr,activityRecomendModel.expireTimeStr];
    }else {
        self.timeLb.text = [NSString stringWithFormat:@"Valid:%@ Until %@",activityRecomendModel.activeTimeStr,activityRecomendModel.expireTimeStr];
    }
    
    //2.标题
    self.titleLb.text = activityRecomendModel.title;
    
    //3.按钮文字
    [self.titleButton setTitle:NSLocalizedString(L_LingQuYouHuiQuan, @"领取优惠券") forState:(UIControlStateNormal)];
    
    //4.是否领取
    if (activityRecomendModel.isReceive) {
        self.titleButton.backgroundColor = [UIColor lightGrayColor];
        self.titleButton.enabled = NO; //禁用
        
        [self.titleButton setTitle:NSLocalizedString(L_YiLingQu, @"已领取") forState:(UIControlStateNormal)];
        [self.titleButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }else {
        self.titleButton.backgroundColor = kMainRedColor;
        self.titleButton.enabled = YES; //起用
    }
}





//!!!!:2019-2-26可用优惠券
- (void)setAvaliableModel:(AvaliableCouponModel *)avaliableModel {
    _avaliableModel = avaliableModel;
    
    
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:avaliableModel.thumbnail] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];
    //标题
    self.titleLb.text = avaliableModel.title;
    

    
    
    //2019-2-26时间
    NSString *endStr = [NSString NYRtimeStrFromTimeStamp:[NSString stringWithFormat:@"%@",avaliableModel.quietDate]];
    NSString *starStr = [NSString NYRtimeStrFromTimeStamp:[NSString stringWithFormat:@"%@",avaliableModel.activeDate]];

    //国际化
    if ([NSString rb_ChineseLanguage]) {
        self.timeLb.text = [NSString stringWithFormat:@"有效期:%@ 至 %@",starStr,endStr];
    }else {
        self.timeLb.text = [NSString stringWithFormat:@"Valid:%@ Until %@",starStr,endStr];
    }
    
    self.titleLb.text = avaliableModel.title;
    
    //更新按钮样式
    NSString *ljsyStr = @"Use Now";
    if ([NSString rb_ChineseLanguage]) {
        ljsyStr = @"立即使用";
    }
    [self.titleButton setTitle:ljsyStr forState:(UIControlStateNormal)];
//2019-2-26不要了
//    //有效期限
//    //时间戳转时间
//    NSString *endStr = [NSString NYRtimeStrFromTimeStamp:[NSString stringWithFormat:@"%@",avaliableModel.quietDate]];
//    NSString *starStr = [NSString NYRtimeStrFromTimeStamp:[NSString stringWithFormat:@"%@",avaliableModel.activeDate]];
}

@end
